<?php

namespace app\lib\tool;

class Arr
{
    /**
     * 无限极分类
     */
    public static function unLimit($list, $pid = 0, $html = 1)
    {
        static $arr = [];
        foreach ($list as $v) {
            if ($v['pid'] == $pid) {
                $v['html'] = $html;

                $prefix = '';
                for ($i = 0; $i < ($html - 1); $i++) {
                    $prefix .= '— ';
                }

                $v['name'] = $prefix . $v['name'];
                $arr[] = $v;
                self::unLimit($list, $v['id'], $html + 1);
            }
        }
        return $arr;
    }

    /**
     * 获取子树
     */
    public static function getChild($data, $id = 0, &$arr = [])
    {
        $arr = [];
        foreach ($data as $v) {
            if ($v['pid'] == $id) {
                $arr[] = $v['id'];
                self::getChild($data, $v['id'], $str);
            }
        }
        $arr[] = $id;
        return $arr;
    }
}
